package com.example.service.impl;

import com.example.dao.CategoryDao;
import com.example.entity.Category;
import com.example.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 分类服务实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Override
    public PageInfo<Category> getCategoryList(Integer pageNum, Integer pageSize, String key) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Category> list = categoryDao.selectAll(map);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Category category) {
        if (category.getId() == null) {
            categoryDao.add(category);
        } else {
            categoryDao.update(category);
        }
    }

    @Override
    public void deleteCategory(int id) {
        categoryDao.delete(id);
    }

    @Override
    public void batchDeleteCategory(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            categoryDao.delete(Integer.parseInt(id));
        }
    }

    @Override
    public List<Category> getAllCategories() {
        return categoryDao.selectAll(null);
    }
} 